<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initModules() {
        set_include_path(get_include_path() . PATH_SEPARATOR . APPLICATION_PATH . '/modules/');

        $auto_loader = Zend_Loader_Autoloader::getInstance();
        $front_controller = $this->bootstrap("frontController")->frontController;

        $modules = $front_controller->getControllerDirectory();
        $controllers = array();
        foreach ($modules as $module => $controller) {
            $module_directory = $front_controller->getModuleDirectory($module);

            $auto_loader->pushAutoloader(new Zend_Application_Module_Autoloader(array(
                'namespace' => ucwords($module),
                'basePath' => $module_directory,
                'resourceTypes' => array(
                    'model' => array(
                        'path' => 'models/',
                        'namespace' => 'Model',
                    )
                )
            )));
            $auto_loader->registerNamespace($module);

            $controllers[strtolower($module)] = $controller;
        }
        $front_controller->setControllerDirectory($controllers);
    }

    protected function _initTimezone() {
        date_default_timezone_set('GMT');
    }

    protected function _initDb()
    {
        $resource = $this->getPluginResource('db');

        $db = $resource->getDbAdapter();

        Zend_Registry::set("db", $db);
    }

    protected function _initAcl() {
        $acl = new Zend_Acl();

        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_GUEST));
        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_PRINCIPAL), Core_Role::ROLE_GUEST);
        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_INPUT), array(Core_Role::ROLE_PRINCIPAL, Core_Role::ROLE_GUEST));
        $acl->addRole(new Zend_Acl_Role(Core_Role::ROLE_ADMIN), array(Core_Role::ROLE_INPUT, Core_Role::ROLE_PRINCIPAL, Core_Role::ROLE_GUEST));

        Zend_Registry::set("acl", $acl);
    }

    protected function _initModuleBootstrap() {
        $front = Zend_Controller_Front::getInstance();
        $modules = $front->getControllerDirectory();
        $default = $front->getDefaultModule();

        foreach ($modules as $module => $directory) {
            if (file_exists($directory . '/../Bootstrap.php')) {
                if ($module === $default) {
                    $bootstrap_class = $default . '_Bootstrap';
                } else {
                    $bootstrap_class = ucfirst($module) . '_Bootstrap';
                }
                if (class_exists($bootstrap_class)) {
                    $bootstrap = new $bootstrap_class($this->getApplication());
                    $bootstrap->bootstrap();
                }
            }
        }
        $front->setControllerDirectory($modules);
    }
    
    public function _initSession() {
        Zend_Session::start();
        $session = new Zend_Session_Namespace($this->getOption("session_namespace"));
        Zend_Registry::set("session", $session);
    }
    
    public function _initTranslater() {
        $this->bootstrap('locale'); 
        $locale = $this->getResource('locale');
        $language = $locale->getLanguage();
        
        $translate = new Zend_Translate ( array (
                'adapter' => 'Csv',
                'content' => APPLICATION_PATH . '/languages/' . $language . '/main.csv',
                'locale'  => $language
        ));
        
        $translate->addTranslation(array(
           'adapter' => 'Csv',
           'content' => APPLICATION_PATH . '/languages/vi/main.csv',
           'locale' => 'vi'
        ));
        
        $translate->setLocale($language);
        
        Zend_Registry::set("translate", $translate);
    }  
    
    public function _initConstants() {
        $upload_path = $this->getOption("upload_path");
        $upload_url = $this->getOption("upload_url");
        define("UPLOAD_PATH", $upload_path);
        define("UPLOAD_URL", $upload_url);
    }
    
    public function _resgiterErrorHandler() {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(array(
            'module'     => 'default',
            'controller' => 'error',
            'action'     => 'error'
        )));
    }
}

